/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import com.cleangwt.bootstrap.client.style.Forms;
import com.cleangwt.uibinder.client.TemplateSupport;
import com.google.gwt.dom.client.ButtonElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.dom.client.TextAreaElement;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A form control panel for building form control groups, each group should contain a prompt label,
 * a control widget and an optional addon widget.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class GroupFormPanel extends ComplexPanel {

  /**
   * The form group builder.
   */
  public static class FormGroupBuilder {
    private static final int DEFAULT_PROMPT_DIV = 4;
    private static final int DEFAULT_ADDON_DIV = 1;
    private GroupFormPanel panel;
    private Element row;
    private Element column;

    private FormGroupBuilder(GroupFormPanel panel, Element row) {
      this.panel = panel;
      this.row = row;
    }

    /**
     * Adds a group with the given prompt, control widget and the default prompotDiv.
     * 
     * @param prompt the prompt
     * @param control the control widget
     * @return this
     */
    public FormGroupBuilder add(String prompt, Widget control) {
      return add(prompt, control, null, DEFAULT_PROMPT_DIV, 0);
    }

    /**
     * Adds a group with the given prompt, control widget and promptDiv. In 12-div grid system, if
     * the given promptDiv is 4, the controlDiv shall be the rest 8.
     * 
     * @param prompt the prompt
     * @param control the control widget
     * @param promptDiv the width div of 12-div
     * @return this
     */
    public FormGroupBuilder add(String prompt, Widget control, int promptDiv) {
      return add(prompt, control, null, promptDiv, 0);
    }

    /**
     * Adds a group with the given prompt, control widget, addon widget and the default prompotDiv
     * and addonDiv.
     * 
     * @param prompt the prompt
     * @param control the control widget
     * @param addon the addon widget
     * @return this
     */
    public FormGroupBuilder add(String prompt, Widget control, Widget addon) {
      return add(prompt, control, addon, DEFAULT_PROMPT_DIV, addon == null ? 0 : DEFAULT_ADDON_DIV);
    }

    /**
     * Adds a group with the given prompt, control widget, promptDiv and addonDiv. In 12-div grid
     * system, if the given promptDiv is 4, the addonDiv is 1, the controlDiv shall be the rest 7.
     * 
     * @param prompt the prompt
     * @param control the control widget
     * @param addon the addon widget
     * @param promptDiv the width div of 12-div
     * @param addonDiv the width div of 12-div
     * @return this
     */
    public FormGroupBuilder add(String prompt, Widget control, Widget addon, int promptDiv,
        int addonDiv) {
      assert prompt != null && control != null && promptDiv > 0 && addonDiv >= 0;
      int controlDiv = 12 - promptDiv - addonDiv;
      this.column = Document.get().createDivElement();
      column.setClassName(Forms.FORM_GROUP_STYLE);
      row.appendChild(column);
      Element promptElement = addPrompot(column, prompt, control);

      GridSystem.setDiv(promptElement, promptDiv);
      GridSystem.setDiv(panel.addGroupControl(column, control), controlDiv);
      if (addon != null) {
        Element helpBlock = panel.addGroupControl(column, addon);
        helpBlock.setClassName(Forms.HELP_BLOCK_STYLE);
        GridSystem.setDiv(helpBlock, addonDiv);
      }
      return this;
    }

    /**
     * Sets the div.
     * 
     * @param lg the div for large screen, 0 to ignore
     * @param md the div for midium screen, 0 to ignore
     * @param sm the div for small screen, 0 to ignore
     * @param xs the div for extra small screen, 0 to ignore
     * @return this
     */
    public FormGroupBuilder div(int md) {
      GridSystem.setDiv(column, md);
      return this;
    }

    /**
     * Sets the div.
     * 
     * @param md the div for midium screen, 0 to ignore
     * @return this
     */
    public FormGroupBuilder div(int lg, int md, int sm, int xs) {
      GridSystem.setDiv(column, lg, md, sm, xs);
      return this;
    }

    /**
     * Sets the offset.
     * 
     * @param lg the offset for large screen, 0 to ignore
     * @param md the offset for midium screen, 0 to ignore
     * @param sm the offset for small screen, 0 to ignore
     * @param xs the offset for extra small screen, 0 to ignore
     * @return this
     */
    public FormGroupBuilder offset(int md) {
      GridSystem.setOffset(column, 0, md, 0, 0);
      return this;
    }

    /**
     * Sets the offset.
     * 
     * @param lg the offset for large screen, 0 to ignore
     * @param md the offset for midium screen, 0 to ignore
     * @param sm the offset for small screen, 0 to ignore
     * @param xs the offset for extra small screen, 0 to ignore
     * @return this
     */
    public FormGroupBuilder offset(int lg, int md, int sm, int xs) {
      GridSystem.setOffset(column, lg, md, sm, xs);
      return this;
    }
  }

  public static class VerticalGroupBuilder {
    private GroupFormPanel panel;
    private Element row;
    private Element column;

    private VerticalGroupBuilder(GroupFormPanel panel, Element row) {
      this.panel = panel;
      this.row = row;
    }

    /**
     * Adds a group with the given prompt, control widget and the default prompotDiv.
     * 
     * @param prompt the prompt
     * @param control the control widget
     * @return this
     */
    public VerticalGroupBuilder add(String prompt, Widget control) {
      return add(prompt, control, null);
    }

    /**
     * Adds a group with the given prompt, control widget and the default prompotDiv.
     * 
     * @param prompt the prompt
     * @param control the control widget
     * @param addon the addon widget
     * @return this
     */
    public VerticalGroupBuilder add(String prompt, Widget control, Widget addon) {
      assert prompt != null && control != null;
      this.column = Document.get().createDivElement();
      column.setClassName(Forms.FORM_GROUP_STYLE);
      row.appendChild(column);
      Element promptElement = addPrompot(column, prompt, control);

      GridSystem.setDiv(promptElement, 12);
      GridSystem.setDiv(panel.addGroupControl(column, control), 12);

      if (addon != null) {
        Element helpBlock = panel.addGroupControl(column, addon);
        helpBlock.setClassName(Forms.HELP_BLOCK_STYLE);
        GridSystem.setDiv(helpBlock, 12);
      }
      return this;
    }

    /**
     * Sets the div.
     * 
     * @param md the div for midium screen, 0 to ignore
     * @return this
     */
    public VerticalGroupBuilder div(int md) {
      GridSystem.setDiv(column, md);
      return this;
    }

    /**
     * Sets the div.
     * 
     * @param lg the div for large screen, 0 to ignore
     * @param md the div for midium screen, 0 to ignore
     * @param sm the div for small screen, 0 to ignore
     * @param xs the div for extra small screen, 0 to ignore
     * @return this
     */
    public VerticalGroupBuilder div(int lg, int md, int sm, int xs) {
      GridSystem.setDiv(column, lg, md, sm, xs);
      return this;
    }

    /**
     * Sets the offset.
     * 
     * @param lg the offset for large screen, 0 to ignore
     * @param md the offset for midium screen, 0 to ignore
     * @param sm the offset for small screen, 0 to ignore
     * @param xs the offset for extra small screen, 0 to ignore
     * @return this
     */
    public VerticalGroupBuilder offset(int md) {
      GridSystem.setOffset(column, 0, md, 0, 0);
      return this;
    }

    /**
     * Sets the offset.
     * 
     * @param lg the offset for large screen, 0 to ignore
     * @param md the offset for midium screen, 0 to ignore
     * @param sm the offset for small screen, 0 to ignore
     * @param xs the offset for extra small screen, 0 to ignore
     * @return this
     */
    public VerticalGroupBuilder offset(int lg, int md, int sm, int xs) {
      GridSystem.setOffset(column, lg, md, sm, xs);
      return this;
    }
  }

  private static Element addPrompot(Element group, String prompt, Widget control) {
    Element lbl = Document.get().createLabelElement();
    lbl.setInnerHTML(prompt);
    lbl.setClassName(Forms.CONTROL_LABEL_STYLE);
    group.appendChild(lbl);
    setLabelFor(lbl, control);
    return lbl;
  }

  /**
   * Sets a label for control widget.
   * 
   * @param lbl the label
   * @param control the control
   */
  private static void setLabelFor(Element lbl, Widget control) {
    String controlTag = control.getElement().getTagName();
    if (controlTag.equalsIgnoreCase(InputElement.TAG)
        || controlTag.equalsIgnoreCase(SelectElement.TAG)
        || controlTag.equalsIgnoreCase(TextAreaElement.TAG)
        || controlTag.equalsIgnoreCase(ButtonElement.TAG)) {
      if (control.getElement().getId() == null || control.getElement().getId().isEmpty()) {
        control.getElement().setId(Document.get().createUniqueId());
      }
      lbl.setAttribute("for", control.getElement().getId());
    }
  }

  /**
   * Creates a form control panel that wraps an existing &lt;div&gt; element of template.
   * 
   * @param template the template
   * @param element the element to be wrapped
   */
  public static GroupFormPanel wrap(TemplateSupport template, Element element) {
    GroupFormPanel widget = new GroupFormPanel(element);
    template.wrap(widget);
    return widget;
  }

  /**
   * Creates a form control panel.
   */
  public GroupFormPanel() {
    setElement(Document.get().createDivElement());
  }

  /**
   * This constructor may be used by subclasses to explicitly use an existing element. This element
   * must be a &lt;div&gt; element.
   * 
   * @param element the element to be used
   */
  protected GroupFormPanel(Element element) {
    setElement(element);
  }

  @Override
  public void add(Widget widget) {
    Element row = prepareRow();
    this.add(widget, row);
  }

  private Element addGroupControl(Element group, Widget control) {
    Element controlDiv = Document.get().createDivElement();
    group.appendChild(controlDiv);
    this.add(control, controlDiv);
    return controlDiv;
  }

  /**
   * Creates a {@link FormGroupBuilder} to build form groups.
   * 
   * @return the {@link FormGroupBuilder}
   */
  public FormGroupBuilder build() {
    Element row = prepareRow();
    row.addClassName(Forms.HORIZONTAL_STYLE);
    return new FormGroupBuilder(this, row);
  }

  /**
   * Creates a {@link VerticalGroupBuilder} to build form groups that laying controls vertically.
   * 
   * @return the {@link VerticalGroupBuilder}
   */
  public VerticalGroupBuilder buildVertical() {
    Element row = prepareRow();
    return new VerticalGroupBuilder(this, row);
  }

  private Element prepareRow() {
    Element row = Document.get().createDivElement();
    getElement().appendChild(row);
    row.addClassName(GridSystem.ROW_STYLE);
    GridSystem.setDiv(row, 12);
    return row;
  }

}